package springboot.com.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import springboot.com.entity.Demo;
import springboot.com.service.DemoService;
import springboot.com.service.impl.DemoJdbcServiceImpl;

@RestController
@RequestMapping("/test")
public class DemoController {
	
	@Resource
	private DemoService demoService;
	
	@Resource
	private DemoJdbcServiceImpl demoJdbcServiceImpl;
   
    /**
     * 返回demo数据:
     * 请求地址：http://127.0.0.1:8080/demo/getDemo
     * @return
     */
    @RequestMapping("/getDemo")
    public Demo getDemo(){
       Demo demo = new Demo();
       demo.setId(1);
       demo.setName("Angel");
       return demo;
    }
    @RequestMapping("/getDemo1")
    public ModelAndView getJsp(){
    	ModelAndView andView = new ModelAndView();
    	andView.setViewName("/error.jsp");
    	ModelAndView mav = new ModelAndView("login");
    	return mav;
    }
    //返回頁面測試成功
    @RequestMapping("/getDemojsp")
    public String getDemojsp(){
    	return "error";
    }
    //異常處理
    @RequestMapping("/zeroException")
    public int zeroException(){
      return 100/0;
    }
    
  //數據庫映射
    @RequestMapping("/tableDemo")
    public int tableDemo(){
    	
      return 1;
    }
    
    @RequestMapping("/saveDemo")
    public String saveDemo(){
       Demo demo = new Demo();
       demo.setName("Angel222");
       demoService.save(demo);
       return "index";
    }
    
    @RequestMapping("/findById")
    public Demo findById(){
    	Demo demo = demoService.findById((long) 2);
    	return demo;
    }
    
    @RequestMapping("/jdbctemplate/findById")
    public Demo jdbctemplateById(Long id){
    	Demo demo = demoJdbcServiceImpl.getById(id);
    	return demo;
    }
    
    @RequestMapping("/helloJsp")
    public String helloJsp(Map<String,Object>map){
    	String hello = "hello jsp";
		System.out.println("HelloController.helloJsp().hello=" + hello);
		map.put("hello",hello);
		return"helloJsp";
    }
    
   
}